<template>
  <div class="main-content">
    <!-- 搜索栏 -->
    <div class="search-box">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="商户账号">
          <el-input v-model="searchForm.shangjiazhanghao" placeholder="商户账号"></el-input>
        </el-form-item>
        <el-form-item label="商户名称">
          <el-input v-model="searchForm.shangjiamingcheng" placeholder="商户名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="shangjiazhanghao" label="商户账号"></el-table-column>
      <el-table-column prop="shangjiamingcheng" label="商户名称"></el-table-column>
      <el-table-column prop="lianxidianhua" label="联系电话"></el-table-column>
      <el-table-column prop="youxiang" label="邮箱"></el-table-column>
      <el-table-column label="操作" width="220">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">修改</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 添加/修改对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="商户账号" prop="shangjiazhanghao">
          <el-input v-model="form.shangjiazhanghao" :disabled="!!form.id"></el-input>
        </el-form-item>
        <el-form-item label="商户名称" prop="shangjiamingcheng">
          <el-input v-model="form.shangjiamingcheng"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="mima" v-if="!form.id">
          <el-input v-model="form.mima" type="password"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="lianxidianhua">
          <el-input v-model="form.lianxidianhua"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="youxiang">
          <el-input v-model="form.youxiang"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchForm: {
        shangjiazhanghao: '',
        shangjiamingcheng: ''
      },
      tableData: [],
      page: 1,
      limit: 10,
      total: 0,
      dialogVisible: false,
      dialogTitle: '',
      form: {
        id: '',
        shangjiazhanghao: '',
        shangjiamingcheng: '',
        mima: '',
        lianxidianhua: '',
        youxiang: ''
      },
      rules: {
        shangjiazhanghao: [
          { required: true, message: '请输入商户账号', trigger: 'blur' }
        ],
        shangjiamingcheng: [
          { required: true, message: '请输入商户名称', trigger: 'blur' }
        ],
        mima: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      const params = {
        page: this.page,
        limit: this.limit,
        ...this.searchForm
      }
      this.$http.get('shangjia/page', {params}).then(({data}) => {
        if (data && data.code === 0) {
          this.tableData = data.data.list
          this.total = data.data.total
        }
      })
    },
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleAdd() {
      this.dialogTitle = '添加商户'
      this.form = {
        id: '',
        shangjiazhanghao: '',
        shangjiamingcheng: '',
        mima: '',
        lianxidianhua: '',
        youxiang: ''
      }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '修改商户'
      this.form = {...row}
      this.dialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('确认删除该商户?', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.delete(`shangjia/delete/${row.id}`).then(({data}) => {
          if (data && data.code === 0) {
            this.$message.success('删除成功')
            this.getList()
          }
        })
      })
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const method = this.form.id ? 'put' : 'post'
          const url = this.form.id ? 'shangjia/update' : 'shangjia/save'
          this.$http[method](url, this.form).then(({data}) => {
            if (data && data.code === 0) {
              this.$message.success(this.form.id ? '修改成功' : '添加成功')
              this.dialogVisible = false
              this.getList()
            }
          })
        }
      })
    },
    handleSizeChange(val) {
      this.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getList()
    }
  }
}
</script>

<style scoped>
.main-content {
  padding: 20px;
}
.search-box {
  margin-bottom: 20px;
}
</style>